﻿using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FubiManger.Controls;
using Model;
using DAL;
using System.Data;
using FubiManger.Cashier;
using System.Windows.Media;
using System.Media;
using System.Web.Script.Serialization;
using Smobiler.Device;

namespace FubiManger.UI
{
    partial class MainForm : Smobiler.Core.Controls.MobileForm
    {
        public MainForm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        /// <summary>
        /// 总订单
        /// </summary>
        List<ms_order> ordlist = new List<ms_order>();
        /// <summary>
        /// 今日订单
        /// </summary>
        List<ms_order> daylist = new List<ms_order>();
        /// <summary>
        /// 昨日订单
        /// </summary>
        List<ms_order> yestodaylist = new List<ms_order>();
        /// <summary>
        /// 待核销订单
        /// </summary>
        List<ms_order> codelist = new List<ms_order>();
        /// <summary>
        /// 实时订单
        /// </summary>
        List<ms_order> hourlist = new List<ms_order>();
        /// <summary>
        /// 实时已完成订单
        /// </summary>
        List<ms_order> hourlist1 = new List<ms_order>();
        /// <summary>
        /// 分类集合
        /// </summary>
        List<ms_classify> classifylist = new List<ms_classify>();
        public string merchantid { get; set; }
        /// <summary>
        /// 用户集合
        /// </summary>
        List<ms_user> uslist = new List<ms_user>();
        /// <summary>
        /// 新用户集合
        /// </summary>
        List<ms_user> newlist = new List<ms_user>();
        /// <summary>
        /// 商户
        /// </summary>
        ms_merchant merchant = new ms_merchant();
        public string adminid { get; set; }
        /// <summary>
        /// 1营业0打烊
        /// </summary>
        bool flag = true;
        private void MainForm_Load(object sender, EventArgs e)
        {
            this.Client.HeartBeatEnable(6000);


            //if (merchant.sales_type == "1")
            //    pancode.Visible = true;

            this.Form.ReadClientData("merchantid", (obj, args) =>
            {

                if (!string.IsNullOrEmpty(args.Value))
                {
                    merchantid = args.Value;
                    merchant = merchantDal.Querybyid(args.Value);
                    Getdata();
                    //if (merchant.turnoff == "1")
                    //{
                    //    lblturnoff.Text = "营业中……";
                    //    flag = true;
                    //}
                    //else
                    //{
                    //    lblturnoff.Text = "已打烊";
                    //    flag = false;
                    //}
                    timhour.Start();
                }
                else
                {
                    LoginForm frm = new LoginForm();
                    this.Form.Show(frm, true, (obk, argsk) =>
                    {
                        merchantid = frm.merchantid;
                        merchant = merchantDal.Querybyid(merchantid);
                        Getdata();
                        if (merchant.turnoff == "1")
                        {
                            lblturnoff.Text = "营业中……";
                            flag = true;
                        }
                        else
                        {
                            lblturnoff.Text = "已打烊";
                            flag = false;
                        }
                        timhour.Start();
                    });
                }

            });

            //this.Form.ReadClientData("merchantid", (obj, args) =>
            //{
            //    try
            //    {
            //        if (!string.IsNullOrEmpty(args.Value))
            //        {
            //            merchantid = args.Value;
            //            merchant = merchantDal.Querybyid(merchantid);
            //            if (merchant.sales_type == "1")
            //                pancode.Visible = true;
            //            Getdata();
            //            timhour.Start();
            //        }
            //        else
            //        {
            //            LoginForm frm = new LoginForm();
            //            this.Form.Show(frm, true, (obk, argsk) =>
            //            {
            //                merchantid = frm.merchantid;
            //                merchant = merchantDal.Querybyid(merchantid);
            //                if (merchant.sales_type == "1")
            //                    pancode.Visible = true;
            //                Getdata();
            //                timhour.Start();
            //            });
            //        }
            //    }
            //    catch (Exception)
            //    {

            //        //throw;
            //    }
            //});

        }

        /// <summary>
        /// 获取数据
        /// </summary>
        public void Getdata()
        {
            if (merchantid == "2")
                daylist = orderDal.QueryDelivery1(merchantid);
            else
                daylist = orderDal.QueryDelivery(merchantid);

            lblnosend.Text = daylist.Count.ToString();
            //lbldaycount.Text = daylist.Count.ToString();//今日订单数量  
            //                                            //codelist = orderDal.Querycodes(merchantid);//查待核销订单
            //                                            //lblnoclare.Text = codelist.Count.ToString();//核销订单数量
            //                                            // lblnosend.Text = hourlist1.Count.ToString();//实时订单数量                                          //hourlist1 = orderDal.Querynosend(merchantid);//查未发货订单
            //if (merchantid == "2")
            //    orderDal.QuerybyAll();
            //else
            if (merchantid == "2")
                hourlist = orderDal.QuerybyHour(merchantid, "1");//未配送订单
            else
                hourlist = orderDal.QuerybyHour(merchantid, "2");//未配送订单
            Getorder(hourlist);

        }


        /// <summary>
        /// 获取订单
        /// </summary>
        private void Getorder(List<ms_order> ordlist)
        {
            //try
            //{
            //if (panord.Controls.Count > 0)
            //    panord.Controls.Clear();
            //panord.Height = ordlist.Count * 120;
            //for (int i = 0; i < ordlist.Count; i++)
            //{
            //    ordercontrol1 con = new ordercontrol1();
            //    con.disksn = ordlist[i].orderdisksn;//桌号
            //    con.ordsn = ordlist[i].order_sn;//订单号
            //    con.ordtime = ordlist[i].add_time;
            //    con.nickname = ordlist[i].user_tel.Substring(0, 3) + "****" + ordlist[i].user_tel.Substring(7, 4);
            //    con.ordid = ordlist[i].id;//编号
            //    con.codestatus = ordlist[i].codestatus; //销售状态
            //    con.stype = "1";
            //    panord.Controls.Add(con);
            //}
            if (lvorder.Rows.Count > 0)
                lvorder.Rows.Clear();
            DataTable table = new DataTable();
            table.Columns.Add("nickname");
            table.Columns.Add("ordid");
            table.Columns.Add("ordsn");
            table.Columns.Add("time");
            table.Columns.Add("u_sage");
            table.Columns.Add("is_online");
            table.Columns.Add("stype");
            table.Columns.Add("reder");
            for (int i = 0; i < ordlist.Count; i++)
            {
                table.Rows.Add(ordlist[i].consignee_name, ordlist[i].id, ordlist[i].order_sn, ordlist[i].add_time, ordlist[i].u_sage, ordlist[i].is_online, "1", ordlist[i].onreder);
            }
            lvorder.DataSource = table;
            lvorder.DataBind();
            //}
            //catch (Exception ex)
            //{

            //    MessageBox.Show(ex.ToString());
            //}
        }
        /// <summary>
        /// 总订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panallord_Press(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 待核销订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pan_code_Press(object sender, EventArgs e)
        {
            lbltype.Text = "待核销订单";
            codelist = orderDal.Querycodes(merchantid);//查待核销订单
            Getorder(codelist);//待核销订单
        }
        /// <summary>
        /// 今日订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pan_today_Press(object sender, EventArgs e)
        {

        }
        int timer = 0;
        private void timhour_Tick(object sender, EventArgs e)
        {
            timer++;
            if (timer == 6)
            {
                timer = 0;
                int count = 0;
                //if (merchantid == "2")
                //{
                //    hourlist = orderDal.QuerybyAll();
                //    count = orderDal.Querycounnoreder();//查所有未读订单
                //}
                //else
                //{
                //    hourlist = orderDal.QuerybyHour(merchantid);//未处理订单
                //    count = orderDal.Querycounnoreder(merchantid);//查商家未读订单
                //}

                //if (count > 0)
                //    this.Client.PlayAudio("333.mp3", @"C:\mp3\");
                //if (hourlist.Count > 0)
                //{

                //}
                if (merchantid == "2")
                {
                    bool flag = false;

                    hourlist = orderDal.QuerybyHour(merchantid, "1");//未配送订单
                    for (int i = 0; i < hourlist.Count; i++)
                    {
                        if (hourlist[i].onreder == "0")
                            flag = true;
                    }
                    if (flag == true)
                        this.Client.PlayAudio("333.mp3", @"C:\mp3\");
                }
                else
                    hourlist = orderDal.QuerybyHour(merchantid, "2");//未配送订单
                Getorder(hourlist);
            }

        }
        /// <summary>
        /// 停止播放语音
        /// </summary>
        public void StopAudio()
        {
            this.Client.StopAudio();
        }
        /// <summary>
        /// 收银
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pancode_Press(object sender, EventArgs e)
        {
            //CashierForm frm = new CashierForm();
            //frm.merchantid = merchantid;//商户ID
            //frm.adminid = adminid;//用户ID
            //this.Form.Show(frm, true);
            //if (flag == true)
            //{
            //    msgcontrol con = new msgcontrol();
            //    con.title = "营业提示";
            //    con.content = "您正在营业中,确定要打烊吗?";
            //    this.Form.ShowDialog(con, (obj, args) =>
            //    {
            //        if (con.flag == true)
            //        {
            //            flag = false;
            //            lblturnoff.Text = "已打烊";
            //            merchantDal.Updateturnoff("0", merchantid);
            //        }
            //    });
            //}
            //else
            //{
            //    msgcontrol con = new msgcontrol();
            //    con.title = "营业提示";
            //    con.content = "您确定现在开始营业吗?";
            //    this.Form.ShowDialog(con, (obj, args) =>
            //    {
            //        if (con.flag == true)
            //        {
            //            flag = true;
            //            lblturnoff.Text = "营业中……";
            //            merchantDal.Updateturnoff("1", merchantid);
            //        }
            //    });
            //}
            DialogOptions options = new DialogOptions(LayoutJustifyAlign.FlexEnd);
            options.Padding = Padding.Empty;
            fresh_statisticscontrol con = new fresh_statisticscontrol();
            con.mid = merchantid;//商户ID
            this.Form.ShowDialog(con, options);
        }

        private void toolmenu_ToolbarItemClick(object sender, ToolbarClickEventArgs e)
        {
            switch (e.Name)
            {
                case "order":
                    tpview.PageIndex = 0;
                    break;
                case "goods":
                    tpview.PageIndex = 1;
                    classifylist = classifyDal.Querybymerid(merchantid);
                    classifylist = classifylist.OrderBy(a => a.sort).ToList();
                    Getclassify();
                    break;
                case "vip":
                    tpview.PageIndex = 2;
                    //if (merchantid == "2")
                    //{
                    //    uslist = user_merchantDal.Querybymerchantid1();//查全部用户
                    //    newlist = user_merchantDal.QueryNewuser1();//查新用户
                    //}
                    //else
                    //{
                    uslist = user_merchantDal.Querybymerchantid(merchantid);//查全部用户
                    newlist = user_merchantDal.QueryNewuser(merchantid);//查新用户
                                                                        // }
                    lblusercount.Text = uslist.Count.ToString() + " 位";//全部用户
                    lblnewusercount.Text = "新增用户：" + newlist.Count.ToString();//新用户
                                                                              //DataTable table = new DataTable();
                                                                              //table.Columns.Add("heard");
                                                                              //table.Columns.Add("username");
                                                                              //table.Columns.Add("date");
                                                                              //table.Columns.Add("userid");
                                                                              //table.Columns.Add("merchantid");
                                                                              //for (int i = 0; i < uslist.Count; i++)
                                                                              //{
                                                                              //    table.Rows.Add(@"C:\fbheard\" + uslist[i].head_portrait, uslist[i].username, uslist[i].register_time, uslist[i].id, merchantid);
                                                                              //    //userveiwcontrol con = new userveiwcontrol();
                                                                              //    //con.cover = uslist[i].head_portrait;//头像
                                                                              //    //con.username = uslist[i].username;//用户名
                                                                              //    //con.time = uslist[i].register_time;//注册时间
                                                                              //    //con.uid = uslist[i].id;//用户ID
                                                                              //    //con.merchantid = merchantid;//商户ID

                    //}
                    //lvuser.DataSource = table;
                    //lvuser.DataBind();
                    break;
                case "user":
                    tpview.PageIndex = 3;
                    ordlist = orderDal.Querybymerchantid(merchantid);
                    lbluordcount.Text = "全部订单" + " (" + ordlist.Count.ToString() + ")";//全部订单
                    if (merchantid == "2")
                        lbluuser.Text = user_merchantDal.Querynewcount(merchantid).ToString();//昨日用户
                    else
                        lbluuser.Text = user_merchantDal.Querynewcount(merchantid).ToString();//昨日用户
                    ms_merchant merchant = merchantDal.Querybyid(merchantid);
                    imgheard.ResourceID = @"C:\fbimage\" + merchant.logo;
                    lblmerchantname.Text = merchant.merchant_name;//商户名称
                    double money = orderDal.Querysdaymoney(merchantid);//今日收益
                    lblumoney.Text = money.ToString();//今日收益
                    yestodaylist = orderDal.Querybyyestoday(merchantid);
                    lbluord.Text = yestodaylist.Count.ToString();//昨日订单
                    break;
            }
        }
        /// <summary>
        /// 订单刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panorder_Refresh(object sender, EventArgs e)
        {
            try
            {
                //panord.Controls.Clear();
            }
            catch (Exception)
            {


            }
            Getdata();
        }
        /// <summary>
        /// 新增分类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnaddclassify_Press(object sender, EventArgs e)
        {

            Addclassifycontrol con = new Addclassifycontrol();
            con.merchantid = merchantid;
            if (classifylist.Count > 0)
            {
                con.sort = Convert.ToInt32(classifylist[classifylist.Count - 1].sort) + 1;
            }
            else
                con.sort = 1;
            this.Form.ShowDialog(con);

        }
        /// <summary>
        /// 分类列表
        /// </summary>
        private void Getclassify()
        {
            DataTable table = new DataTable();
            table.Columns.Add("title");
            table.Columns.Add("sort");
            table.Columns.Add("classid");
            for (int i = 0; i < classifylist.Count; i++)
            {
                table.Rows.Add(classifylist[i].title, (i + 1).ToString(), classifylist[i].id);
            }
            dgclassview.DataSource = table;
            dgclassview.DataBind();
        }
        /// <summary>
        /// 刷新分类列表
        /// </summary>
        public void Resfhclassify()
        {
            List<ms_classify> classifies = classifyDal.Querybymerid(merchantid);
            classifylist = classifies.OrderBy(a => a.sort).ToList();
            Getclassify();
        }

        private void dgclassview_CellChanged(object sender, DragViewCellChangedEventArgs e)
        {
            for (int i = 0; i < dgclassview.Cells.Count; i++)
            {
                MobileUserControl userControl = (MobileUserControl)dgclassview.Cells[i].Control;
                Panel pan = (Panel)userControl.Controls[0];
                Label lbl = (Label)pan.Controls[1];
                lbl.Text = (i + 1).ToString();
                Label lb2 = (Label)pan.Controls[3];
                classifyDal.Updatesort(lbl.Text, lb2.Text);
            }

            //Toast(((DataRowView)dgclassview.Cells..Control.TemplateData)[1].ToString());
        }
        /// <summary>
        /// 刷新分类
        /// </summary>
        public void Refshclass()
        {
            List<ms_classify> classifies = classifyDal.Querybymerid(merchantid);
            classifylist = classifies.OrderBy(a => a.sort).ToList();
            Getclassify();
        }
        private void dgclassview_CellBind(object sender, DragViewTemplateBindEventArgs e)
        {
            //if (i % 2 == 0)
            //    e.Cell.Control.BackColor = System.Drawing.Color.AliceBlue;
            //i++;
        }
        /// <summary>
        /// 商品管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btngoods_Press(object sender, EventArgs e)
        {
            GoodsForm frm = new GoodsForm();
            frm.merchantid = merchantid;//商户ID
            this.Form.Show(frm, true);
        }
        /// <summary>
        /// 商城预览
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnshop_Press(object sender, EventArgs e)
        {
            BusinessForm frm = new BusinessForm();
            frm.merchantid = merchantid;//商家ID
            this.Form.Show(frm, true);
        }
        /// <summary>
        /// 商品上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panaddgood_Press(object sender, EventArgs e)
        {
            if (classifylist.Count > 0)
            {
                AddgoodForm frm = new AddgoodForm();
                frm.merchantid = merchantid;
                this.Form.Show(frm, true);
            }
            else
            {
                MessageBox.Show("未增加分类请先添加分类");
            }
        }
        /// <summary>
        /// 查看新用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pan_newuser_Press(object sender, EventArgs e)
        {
            NewusersForm frm = new NewusersForm();
            frm.uslist = newlist;
            frm.merchantid = merchantid;
            this.Form.Show(frm, true);
        }
        /// <summary>
        /// 金币比例
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panintegral_Press(object sender, EventArgs e)
        {
            IntegralForm frm = new IntegralForm();
            frm.merchantid = merchantid;
            this.Form.Show(frm, true);
        }
        /// <summary>
        /// 店铺设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panusedit_Press(object sender, EventArgs e)
        {
            EditmanngerForm frm = new EditmanngerForm();
            frm.merchantid = merchantid;
            this.Form.Show(frm, true);
        }
        /// <summary>
        /// 总订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panords_Press(object sender, EventArgs e)
        {
            timhour.Stop();
            OrdersForm frm = new OrdersForm();
            frm.merchantid = merchantid;
            this.Form.Show(frm, true, (obj, args) =>
            {
                timhour.Start();
            });
        }
        /// <summary>
        /// 活动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panuactvi_Press(object sender, EventArgs e)
        {
            ActivitysForm frm = new ActivitysForm();
            frm.merchantid = merchantid;
            this.Form.Show(frm, true);
        }
        /// <summary>
        /// 红包
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panmarket_Press(object sender, EventArgs e)
        {
            //PacketForm frm = new PacketForm();
            //frm.merchantid = merchantid;
            //this.Form.Show(frm, true);
        }
        /// <summary>
        /// 设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panedit_Press(object sender, EventArgs e)
        {
            this.Form.Show(new EditForm(), true);
        }
        /// <summary>
        /// 消费券
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panucoupon_Press(object sender, EventArgs e)
        {
            CouponForm frm = new CouponForm();
            frm.merchantid = merchantid;
            this.Form.Show(frm, true);
        }
        int srot = 1;
        /// <summary>
        /// 时间排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btndate_Press(object sender, EventArgs e)
        {
            if (btndate.Text == "时间降序")
            {
                srot = 2;
                List<ms_order> ordlist = hourlist.OrderBy(a => a.id).ToList();//升序
                btndate.Text = "时间升序";
                Getorder(ordlist);
            }
            else
            {
                srot = 1;
                List<ms_order> ordlist = hourlist.OrderByDescending(a => a.id).ToList();//降序
                btndate.Text = "时间降序";
                Getorder(ordlist);
            }
        }
        /// <summary>
        /// 打开扫描
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panscan_Press(object sender, EventArgs e)
        {
            bsanner.GetBarcode();
        }

        private void bsanner_BarcodeScanned(object sender, BarcodeResultArgs e)
        {
            try
            {
                if (e.isError == false)
                {
                    Array temp1 = e.Value.Split('&');
                    Array temp2 = temp1.GetValue(1).ToString().Split('=');//获取商户信息
                    string me_rchantid = temp2.GetValue(1).ToString();//获取商户编号
                    Array temp4 = temp1.GetValue(0).ToString().Split('=');//获取订单信息
                    string ordsn = temp4.GetValue(1).ToString();//获取订单号
                    Array temp5 = temp1.GetValue(2).ToString().Split('=');//获取核销信息
                    string status = temp5.GetValue(1).ToString();//获取核销状态
                    if (status == "1")
                    {
                        MessageBox.Show("该订单已核销");
                    }
                    else
                    {
                        if (merchantid != me_rchantid)
                        {
                            MessageBox.Show("该订单不属于您");
                        }
                        else
                        {
                            MessageBox.Show("点击确定将核销订单", "核销提示", MessageBoxButtons.OKCancel, (obj, args) =>
                            {
                                if (orderDal.Updatecodestatusscan(ordsn) > 0)//扫码核销订单
                                {
                                    Toast("订单核销成功");
                                    Getdata();
                                }
                            });
                        }
                    }
                }
            }
            catch (Exception)
            {

                Toast("二维码无法识别");
            }
        }
        /// <summary>
        /// 昨日订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panuord_Press(object sender, EventArgs e)
        {
            MainorderForm frm = new MainorderForm();
            frm.ordertype = "3";
            frm.merchantid = merchantid;
            frm.title = "昨日订单";
            this.Form.Show(frm, true, (obj, args) =>
            {
                //daylist = orderDal.Querybytoday(merchantid);//今日订单

                //lbldayord.Text = daylist.Count.ToString();
            });
        }

        private void lvorder_Refresh(object sender, EventArgs e)
        {
            Getdata();//刷新数据
        }

        private void MainForm_KeyDown(object sender, KeyDownEventArgs e)
        {
            if (e.KeyCode == KeyCode.Back)
            {

            }
        }
        /// <summary>
        /// 未核销订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pan_clare_Press(object sender, EventArgs e)
        {
            lbltype.Text = "未核销订单";
            codelist = orderDal.Querycodes(merchantid);//查待核销订单
            lblnoclare.Text = codelist.Count.ToString();//核销订单数量
            Getorder(codelist);
        }
        /// <summary>
        /// 未发货
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pannosend_Press(object sender, EventArgs e)
        {
            lbltype.Text = "未发货订单";
            hourlist1 = orderDal.Querynosend(merchantid);//查未发货订单
            lblnosend.Text = hourlist1.Count.ToString();//未发货订单数量
            Getorder(codelist);
        }

        private void btndayfood_Press(object sender, EventArgs e)
        {
            AdddayfoodForm frm = new AdddayfoodForm();
            frm.merchantid = merchantid;
            this.Form.Show(frm, true);
        }
        /// <summary>
        /// 出餐下单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnsend_Press(object sender, EventArgs e)
        {
            DialogOptions options = new DialogOptions(LayoutJustifyAlign.FlexEnd);
            options.Padding = Padding.Empty;
            statisticsDelivecontrol con = new statisticsDelivecontrol();
            con.merchantid = merchantid;
            this.Form.ShowDialog(con, options, (obj, args) =>
            {
                hourlist = orderDal.QuerybyHour(merchantid, "2");//未处理订单
                Getorder(hourlist);
            });

        }
        /// <summary>
        /// 打印小票
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnprint_Press(object sender, EventArgs e)
        {
            msgcontrol con = new msgcontrol();
            con.title = "打印提示";
            con.content = "该功能将打印所有未处理小票，确定要打印吗？";
            this.Form.ShowDialog(con, (obj, args) =>
            {
                if (con.flag == true)
                {
                    hourlist = orderDal.QuerybyHour(merchantid, "2");//未处理订单
                    if (hourlist.Count > 0)
                    {
                        ms_merchant merchant = merchantDal.Querybyid(hourlist[0].merchant_id);
                        List<string> Printlist = new List<string>();
                        for (int i = 0; i < hourlist.Count; i++)
                        {

                            string printtxt = merchant.merchant_name + "\r\n";
                            string str2 = "收货人：" + hourlist[i].consignee_name;
                            string str6 = "电话：" + hourlist[i].consignee_tel.Substring(0, 3) + "*****" + hourlist[i].consignee_tel.Substring(8, 3);
                            string str3 = "---------------------";
                            string str4 = "时间：" + hourlist[i].add_time;
                            string str5 = "编号：" + hourlist[i].order_sn.Substring(8);
                            string str7 = "---------------------";
                            string str10 = "---------------------";
                            string str11 = "优惠：-￥" + hourlist[i].coupon_amount;
                            string str12 = "配送：￥" + hourlist[i].logistics_amount;
                            string str13 = "打包：￥" + hourlist[i].packing_amount;
                            string str14 = "实付：￥" + hourlist[i].order_price;
                            string str15 = "星鲜厨 正儿八经吃个饭";
                            string str16 = "备注：￥" + hourlist[i].orderremark;
                            List<string> strlist = new List<string>();
                            strlist.Add(str2);
                            strlist.Add(str3);
                            strlist.Add(str4);
                            strlist.Add(str5);
                            strlist.Add(str6);
                            strlist.Add(str7);
                            JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
                            List<Gdpro> prolist = js.Deserialize<List<Gdpro>>(hourlist[i].goods_info);

                            for (int k = 0; k < prolist.Count; k++)
                            {
                                //string proname = "";

                                //int totalLength = 8 - prolist[i].proname.Length; // 指定字符串加空白后的总长度
                                //switch (totalLength)
                                //{
                                //    case 1:
                                //        proname = prolist[i].proname + "  ";
                                //        break;
                                //    case 2:
                                //        proname = prolist[i].proname + "    ";
                                //        break;
                                //    case 3:
                                //        proname = prolist[i].proname + "       ";
                                //        break;
                                //    case 4:
                                //        proname = prolist[i].proname + "          ";
                                //        break;
                                //    case 5:
                                //        proname = prolist[i].proname + "           ";
                                //        break;
                                //}
                                //string pstr = proname + "x" + prolist[i].count + " ￥" + prolist[i].price;
                                //strlist.Add(pstr);
                                string proname = "";
                                char paddingChar = ' '; // 指定空白字符为空格
                                int totalLength = 8 - prolist[i].proname.Length; // 指定字符串加空白后的总长度
                                if (prolist[i].proname.Length < 8)
                                    proname = prolist[k].proname.PadRight(totalLength, paddingChar);
                                string pstr = proname + "  x" + prolist[k].count + " ￥" + prolist[k].price;
                                strlist.Add(pstr);
                            }

                            strlist.Add(str10);
                            strlist.Add(str11);
                            strlist.Add(str12);
                            strlist.Add(str13);
                            strlist.Add(str14);
                            strlist.Add(str15);
                            if (!string.IsNullOrEmpty(hourlist[i].orderremark))
                                strlist.Add(str16);

                            for (int j = 0; j < strlist.Count; j++)
                            {
                                //count = (i + 1) * 35;
                                printtxt += strlist[j] + "\r\n";
                                // Printlist.Add(printtxt);
                            }
                            Print(printtxt);
                        }
                        //for (int i = 0; i < Printlist.Count; i++)
                        //{
                        //    Print(Printlist[i]);//打印
                        //}
                    }
                    else
                    {
                        MessageBox.Show("暂无预定可打印小票");
                    }
                }
            });
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="printxt"></param>
        private void Print(Object printxt)
        {

            P800PrintEntityCollection entityblueslection = new P800PrintEntityCollection();

            //文本打印
            P800PrintTextEntity text = new P800PrintTextEntity()
            {
                Content = printxt.ToString(),
                Offset = P800PrintOffSet.Left,
                IsBold = false,
                CharacterMultipleX = 1,
                CharacterMultipleY = 0,
                IsUnderLine = false,

            };
            entityblueslection.Add(text);
            //第一个参数：打印指令集，第二个参数：是否切纸，第三个参数：回调
            p800Printer1.Print(entityblueslection, true, (obj, args) =>
            {
                if (args.isError == true)
                    this.MessageBox.Show("Error: " + args.error);
                else
                    this.MessageBox.Show("success");

            });
        }
    }
}